//重写alert
window.alert = function(msg, callback, arg){
	layer.alert(msg, function(index){
		layer.close(index);
		if(typeof(callback) === "function"){
			callback(arg);
		}
	});
};

//重写confirm式样框
//window.confirm = function(msg, callback){
//	layer.confirm(msg, {btn: ['确定','取消']},
//	function(){//确定事件
//		if(typeof(callback) === "function"){
//			callback("ok");
//		}
//	});
//};

function passwordCheck(str)
{
	//var format= /^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{6,10}$/; 
	var format= /^[a-zA-Z0-9]{6,10}$/;
	if(format.test(str))
	{
		return true;
	}
	return false;
}

function userNameCheck(str)
{
	var format= /^[a-zA-Z][a-zA-Z0-9]{1,10}$/; 
	if(format.test(str))
	{
		return true;
	}
	return false;
}

function realNameCheck(str)
{
	var format= /^[\u4E00-\u9FA5\uf900-\ufa2d\w]{1,20}$/; //中文英文数字
	if(format.test(str))
	{
		return true;
	}
	return false;
}

function projectCodeCheck(str)
{
	var format= /^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{1,30}$/; 
	if(format.test(str))
	{
		return true;
	}
	return false;
}

function sectionNoCheck(str)
{
	var format= /^\d{6}$/; 
	if(format.test(str))
	{
		return true;
	}
	return false;
}

function advanceNumCheck(str)
{
	var format= /^([1-9]|[12][0-9]|30)$/;
	if(format.test(str))
	{
		return true;
	}
	return false;
}

function zipCodeCheck(str)
{
	var format= /^[0-9]\d{5}$/; 
	if(format.test(str))
	{
		return true;
	}
	return false;
}
//时间yyyy-MM-dd验证
function checkDate(str)
{
	var format= /^(?:19|20)[0-9][0-9]-(?:(?:0[1-9])|(?:1[0-2]))-(?:(?:[0-2][1-9])|(?:[1-3][0-1]))$/; 
	if(format.test(str))
	{
		return true;
	}
	return false;
}

//半角转换为全角函数 
function toDBC(txtstring) {
	var tmp = "";
	for (var i = 0; i < txtstring.length; i++) {
		if (txtstring.charCodeAt(i) == 32) {
			tmp = tmp + String.fromCharCode(12288);
		}
		if (txtstring.charCodeAt(i) < 127) {
			tmp = tmp + String.fromCharCode(txtstring.charCodeAt(i) + 65248);
		}
	}
	return tmp;
}
//全角转换为半角函数
function toCDB(str) {
	var tmp = "";
	for (var i = 0; i < str.length; i++) {
		if (str.charCodeAt(i) > 65248 && str.charCodeAt(i) < 65375) {
			tmp += String.fromCharCode(str.charCodeAt(i) - 65248);
		} else {
			tmp += String.fromCharCode(str.charCodeAt(i));
		}
	}
	return tmp;
}

//输入限制   add --  xushuo
$(function(){
	$(".J_onlyNumberXZ").on('keyup blur',function(){
		onlyNumberAuto($(this));
	});
	$(".J_onlyNumberXZ").on('paste',function(){
		var me=$(this);
		setTimeout(function(){
			onlyNumberAuto(me);
		},50);
	});
	function onlyNumberAuto(me){
		var xz=me.attr("data-xz").split(",");
		var q=parseInt(xz[0]);
		var h=parseInt(xz[1]);
		var val=me.val();
		var val1=val;
		if(q==1){
			val1 = val1.replace(/^\d{2,}/,val1.substr(0,1));
		}else if(q==2){
			val1 = val1.replace(/^\d{3,}/,val1.substr(0,2));
		}else if(q==3){
			val1 = val1.replace(/^\d{4,}/,val1.substr(0,3));
		}else if(q==4){
			val1 = val1.replace(/^\d{5,}/,val1.substr(0,4));
		}else if(q==5){
			val1 = val1.replace(/^\d{6,}/,val1.substr(0,5));
		}else if(q==6){
			val1 = val1.replace(/^\d{7,}/,val1.substr(0,6));
		}else if(q==7){
			val1 = val1.replace(/^\d{8,}/,val1.substr(0,7));
		}else if(q==8){
			val1 = val1.replace(/^\d{9,}/,val1.substr(0,8));
		}else if(q==9){
			val1 = val1.replace(/^\d{10,}/,val1.substr(0,9));
		}else if(q==10){
			val1 = val1.replace(/^\d{11,}/,val1.substr(0,10));
		}else if(q==11){
			val1 = val1.replace(/^\d{12,}/,val1.substr(0,11));
		}else if(q==12){
			val1 = val1.replace(/^\d{13,}/,val1.substr(0,12));
		}
		
		val1 = val1.replace(/[^\d.]/g,"");
		val1 = val1.replace(/^\./g,"");
		val1 = val1.replace(/\.{2,}/g,".");
		val1 = val1.replace(".","$#$").replace(/\./g,"").replace("$#$",".");
		if(h==1){
			val1 = val1.replace(/^(\-)*(\d+)\.(\d).*$/,'$1$2.$3');
		}else if(h==2){
			val1 = val1.replace(/^(\-)*(\d+)\.(\d\d).*$/,'$1$2.$3');
		}else if(h==3){
			val1 = val1.replace(/^(\-)*(\d+)\.(\d\d\d).*$/,'$1$2.$3');
		}else if(h==4){
			val1 = val1.replace(/^(\-)*(\d+)\.(\d\d\d\d).*$/,'$1$2.$3');
		}else if(h==5){
			val1 = val1.replace(/^(\-)*(\d+)\.(\d\d\d\d\d).*$/,'$1$2.$3');
		}
		
		val1 = val1.replace(/^[0]\d+/g,parseInt(val1));
		if(val1!=val){
			me.val(val1);
		}
	}
	
	/*只允许输入数字*/
	$(".J_onlyNumber73").on('keyup blur',function(){
		onlyNumber73($(this));
	});
	$(".J_onlyNumber73").on('paste',function(){
		var me=$(this);
		setTimeout(function(){
			onlyNumber73(me);
		},50);
	});
	function onlyNumber73(me){
		var val=me.val();
		var val1=val;
		val1 = val1.replace(/^\d{8,}/,val1.substr(0,7));
		val1 = val1.replace(/[^\d.]/g,"");
		val1 = val1.replace(/^\./g,"");
		val1 = val1.replace(/\.{2,}/g,".");
		val1 = val1.replace(".","$#$").replace(/\./g,"").replace("$#$",".");
		val1 = val1.replace(/^(\-)*(\d+)\.(\d\d\d).*$/,'$1$2.$3');
		
		val1 = val1.replace(/^[0]\d+/g,parseInt(val1));
		if(val1!=val){
			me.val(val1);
		}
	}
	
	
	/*只允许输入数字*/
	$(".J_onlyNumber").on('keyup blur',function(){
		onlyNumber($(this));
	});
	$(".J_onlyNumber").on('paste',function(){
		var me=$(this);
		setTimeout(function(){
			onlyNumber(me);
		},50);
	});
	function onlyNumber(me){
		var val=me.val();
		var val1=val;
		var dd=parseInt(me.attr("data-d"));
		
		var d1=/^(\-)*(\d+)\.(\d).*$/;
		var d2=/^(\-)*(\d+)\.(\d\d).*$/;
		var d3=/^(\-)*(\d+)\.(\d\d\d).*$/;
		var d4=/^(\-)*(\d+)\.(\d\d\d\d).*$/;
		var d5=/^(\-)*(\d+)\.(\d\d\d\d\d).*$/;
		var d6=/^(\-)*(\d+)\.(\d\d\d\d\d\d).*$/;
		var d=/^(\-)*(\d+)\.(\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d).*$/;
		var myDD=d;
		if(dd==1){
			myDD=d1;
		}else if(dd==2){
			myDD=d2;
		}else if(dd==3){
			myDD=d3;
		}else if(dd==4){
			myDD=d4;
		}else if(dd==5){
			myDD=d5;
		}else if(dd==6){
			myDD=d6;
		}
		
		val1 = val1.replace(/[^\d.]/g,"");
		val1 = val1.replace(/^\./g,"");
		val1 = val1.replace(/\.{2,}/g,".");
		val1 = val1.replace(".","$#$").replace(/\./g,"").replace("$#$",".");
		val1 = val1.replace(myDD,'$1$2.$3');
		
		val1 = val1.replace(/^[0]\d+/g,parseInt(val1));
		if(val1!=val){
			me.val(val1);
		}
	}
	
	
	//只能正整数
	$(".J_onlyInit").on('keyup blur',function(){
		onlyInit($(this));
	});
	$(".J_onlyInit").on('paste',function(){
		var me=$(this);
		setTimeout(function(){
			onlyInit(me);
		},50);
	});
	function onlyInit(me){
		var val=me.val();
		var val1;
		//val=val.replace(/\D/g,'');
		if(val.length==1){//文本框只能输入正整数
			val1=val.replace(/[^1-9]/g,'');
		}else{
			val1=val.replace(/\D/g,'');
		}
		if(val1!=val){
			me.val(val1);
		}
	}
	
	
	//最大长度
	$(".J_maxLen").on('keyup blur',function(){
		maxLen($(this));
	});
	$(".J_maxLen").on('paste',function(){
		var me=$(this);
		setTimeout(function(){
			maxLen(me);
		},50);
	});
	function maxLen(me){
		var val=me.val();
		var m=parseInt(me.attr("data-max"));
		var val1=val.substr(0,m);
		
		if(val1!=val){
			me.val(val1);
		}
	}
	
	
	//不能有空格
	$(".J_noSp").on('keyup blur',function(){
		noSp($(this));
	});
	$(".J_noSp").on('paste',function(){
		var me=$(this);
		setTimeout(function(){
			noSp(me);
		},50);
	});
	function noSp(me){
		var val=me.val();
		var val1=val.replace(/\s/g,"")
		if(val1!=val){
			me.val(val1);
		}
	}
	
	
	//只能输入数字和字母
	$(".J_onlyNumLet").on('keyup blur',function(){
		onlyNumLet($(this));
	});
	$(".J_onlyNumLet").on('paste',function(){
		var me=$(this);
		setTimeout(function(){
			onlyNumLet(me);
		},50);
	});
	function onlyNumLet(me){
		var val=me.val();
		var val1=val.replace(/[^\w]/ig,"");
		val1=val1.replace(/_/ig,"");
		if(val1!=val){
			me.val(val1);
		}
	}
	
	//不能输入中文
	$(".J_unit").on('keyup blur',function(){
		onLyunit($(this));
	});
	$(".J_unit").on('paste',function(){
		var me=$(this);
		setTimeout(function(){
			onLyunit(me);
		},50);
	});
	function onLyunit(me){
		var val=me.val();
		var val1=val.replace(/[\u4E00-\u9FA5]/ig,"");
		//val1=val1.replace(/[。.，,<>《》?？、/;；'‘“”":【】\\[\]{}{}||·~`~!！@#￥$%……^&*()（）\-_——=+]/ig,"");
		//var zz="/[。.，,<>《》?？、/;；'‘“”\":【】\\[\]{}{}||·~`!！@#￥$%……^&*()（）\-_——=+]/ig";
		val1=val1.replace(/[。.，,<>《》?？、\/;；'‘“”":【】\\[\]{}{}||·~`!！@#￥$%……^&*()（）\-_——=+]/ig,"");
		if(val1!=val){
			me.val(val1);
		}
	}
	
	//只允许输入数字，可以0开头，不能包含小数
	$(".J_autoNum").on('keyup blur',function(){
		autoNum($(this));
	});
	$(".J_autoNum").on('paste',function(){
		var me=$(this);
		setTimeout(function(){
			autoNum(me);
		},50);
	});
	function autoNum(me){
		var val=me.val();
		val1=val.replace(/[^0-9]/g,'');
		if(val1!=val){
			me.val(val1);
		}
	}
});

